package com.wechat.server.mp.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.wechat.server.mp.dto.OrderExcelModel;
import com.wechat.server.mp.service.PddService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * @description: 导出
 * @author: stone
 * @date: Created by 2021/10/10 14:32
 * @version: 1.0.0
 * @pakeage: com.wechat.server.mp.controller
 */
@Controller
@RequestMapping("/excel")
@Slf4j
public class ExcelController {


    @Autowired
    private PddService pddService;

    @GetMapping("/export")
    public ModelAndView excel(ModelMap modelMap) {
        log.info("模板 ====> ");
        return new ModelAndView("excel");
    }

    @GetMapping("/wwww")
    public String a() {
        return "1122";
    }

    @GetMapping("/excel")
    public void excel(HttpServletResponse response,
                        @RequestParam(value = "num", required = false) Integer num,
                        @RequestParam(value = "page", defaultValue = "1") Integer page) throws Exception {
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.addHeaderAlias("orderSn", "订单号");
        writer.addHeaderAlias("skuName", "商品名称");
        writer.addHeaderAlias("goodsSpec", "商品规格");
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("idCard", "身份证号");
        writer.addHeaderAlias("phone", "联系电话");
        writer.addHeaderAlias("address", "收货地址");
        //writer.merge(1, "员工信息表");
        List<OrderExcelModel> orderDataList = pddService.getOrderDataList(num, page);
        writer.write(orderDataList, true);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");

        String format = DateUtil.format(new Date(), "yyyy-MM-dd");
        String name = "订单列表_" + format + "_" + System.currentTimeMillis();

        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8") + ".xls" );
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            writer.flush(out, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.close();
        }
        IoUtil.close(out);

    }

    @GetMapping("/code")
    public String getAccessToke(@RequestParam(value = "code", required = false) String code,ModelMap modelMap) throws Exception {
        log.info("第三方请求到的code ====> {}", code);

        String result = pddService.getAccessToke(code);

        modelMap.addAttribute("token", result);
        return "redirect:/excel/export";
    }
}
